package local.blueheart.data.springdatademo.domain.system;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
public class AppUser extends AbstractDomainClass {

    private String username;
    @Transient
    private String password;

    private String encryptedPassword;

    private Boolean enabled = true;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "APP_USER_ROLE", joinColumns = @JoinColumn(name = "user_id"), inverseJoinColumns = @JoinColumn(name = "role_id"))
    private List<AppRole> appRoles = new ArrayList<>();

    private Integer failedLoginAttempts = 0;

    public AppUser(){

    }

    public AppUser(String username, String password, String encryptedPassword) {
        this.username = username;
        this.password = password;
        this.encryptedPassword = encryptedPassword;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getEncryptedPassword() {
        return encryptedPassword;
    }

    public void setEncryptedPassword(String encryptedPassword) {
        this.encryptedPassword = encryptedPassword;
    }

    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }


    public List<AppRole> getAppRoles() {
        return appRoles;
    }

    public void setAppRoles(List<AppRole> appRoles) {
        this.appRoles = appRoles;
    }

    public void addRole(AppRole appRole) {
        if (!this.appRoles.contains(appRole)) {
            this.appRoles.add(appRole);
        }

        if (!appRole.getAppUsers().contains(this)) {
            appRole.getAppUsers().add(this);
        }
    }

    public void removeRole(AppRole appRole) {
        this.appRoles.remove(appRole);
        appRole.getAppUsers().remove(this);
    }

    public Integer getFailedLoginAttempts() {
        return failedLoginAttempts;
    }

    public void setFailedLoginAttempts(Integer failedLoginAttempts) {
        this.failedLoginAttempts = failedLoginAttempts;
    }
}